/**
 *
 */
package org.cholm.drtest;

import org.json.JSONObject;

/**
 * A particular broadcast
 *
 * @author cholm
 */
public class Program  {

	/** Index of low resolution URL */
	public static final int LOW = 0;
	/** Index of medium resolution URL */
	public static final int MEDIUM = 1;
	/** Index of high resolution url */
	public static final int HIGH = 2;

	/**
	 * Create a video object from a JSON object
	 *
	 * @param json JSON object to decode
	 */
	public Program(JSONObject json) {
		id    = json.optString("id");
		title = json.optString("title");
		date  = json.optString("formattedBroadcastTime");
	}
	/**
	 * Check if this is valid
	 *
	 * @param testStreams if true, also test links
	 *
	 * @return true if valid
	 */
	public boolean isValid(boolean testStreams)
	{
		if (id    == null || id.isEmpty()) return false;
		if (testStreams && !hasLink()) return false;
		return true;
	}
	/**
	 * Check if any link is set
	 *
	 * @return true if at least one link is set
	 */
	public boolean hasLink() {
		if (urls[LOW] == null &&
			urls[MEDIUM] == null &&
			urls[HIGH]   == null) return false;
		return true;
	}
	/**
	 * Massage link to media over HTTP
	 *
	 * @param url Input RTSP url
	 *
	 * @return HTTP url
	 */
	protected String makeLink(String url)
	{
		return "http://vodfiles.dr.dk/" +
				url.substring(url.indexOf("CMS/Resources/"), url.length());
	}
	/**
	 * Set the links
	 *
	 * @param streams JSON object with information
	 */
	public void setLinks(JSONObject streams) {
		urls[Program.LOW]     = makeLink(streams.optString("low"));
		urls[Program.MEDIUM]  = makeLink(streams.optString("medium"));
		urls[Program.HIGH]    = makeLink(streams.optString("high"));
	}
	/**
	 * @return the id
	 */
	public String getId() { return id; }
	/**
	 * @return the slug
	 */
	public String getTitle() { return title; }
	/**
	 * @return the desc
	 */
	public String getDate() { return date; }
	/**
	 * @return the mani
	 */
	public String getStream(int qual) { return urls[qual]; }
	/** Video ID */
	protected String id = null;
	/** Title */
	protected String title = null;
	/** When broadcasted */
	protected String date = null;
	/** streams */
	protected String[] urls = { null, null, null };
}

/* EOF */